package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yk/api/request"
	"yk/internal/dao"
	"yk/internal/model"
)

func AddVideo(c *gin.Context) {
	var req request.AddVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var user model.User

	dao.GetOneByFields(&model.User{
		Id: c.GetUint("userId"),
	}, &user)

	var video model.Video
	if user.UType != 3 {
		video = model.Video{

			VName:      req.CName,
			Author:     c.GetUint("userId"),
			AuthorNum:  req.AuthorNum,
			MajorOne:   req.MajorOne,
			MajorTwo:   req.MajorTwo,
			CName:      req.VName,
			CNum:       req.CNum,
			CTime:      req.CTime,
			Cover:      req.Cover,
			QrCode:     req.QrCode,
			Skey:       req.Skey,
			Describe:   req.Describe,
			LookRange:  req.LookRange,
			EnableType: req.EnableType,
		}
		if !dao.Create(&video) {
			c.JSON(http.StatusOK, gin.H{
				"code": 1000,
				"msg":  "普通员工视频上传失败",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "普通员工视频上传成功",
			"data": video,
		})
	} else {
		video = model.Video{

			VName:      req.CName,
			Author:     c.GetUint("userId"),
			AuthorNum:  req.AuthorNum,
			MajorOne:   req.MajorOne,
			MajorTwo:   req.MajorTwo,
			CName:      req.VName,
			CNum:       req.CNum,
			CTime:      req.CTime,
			Cover:      req.Cover,
			QrCode:     req.QrCode,
			Skey:       req.Skey,
			Describe:   req.Describe,
			LookRange:  req.LookRange,
			EnableType: req.EnableType,
		}
		if !dao.Create(&video) {
			c.JSON(http.StatusOK, gin.H{
				"code": 1000,
				"msg":  "管理员视频上传失败",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "管理员视频上传成功",
			"data": video,
		})
	}

}
func InfoVideo(c *gin.Context) {
	var req request.InfoVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var video model.Video
	if !dao.GetOneById(req.Id, &video) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "视频详情展示失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频详情展示成功",
		"data": video,
	})

}

func UpdateVideo(c *gin.Context) {
	var req request.UpdateVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var user model.User

	dao.GetOneByFields(&model.User{
		Id: c.GetUint("userId"),
	}, &user)
	var video model.Video
	if user.UType != 3 {
		video = model.Video{
			Id:          req.Id,
			VName:       req.VName,
			Author:      c.GetUint("userId"),
			AuthorNum:   req.AuthorNum,
			MajorOne:    req.MajorOne,
			MajorTwo:    req.MajorTwo,
			CName:       req.CName,
			CNum:        req.CNum,
			CTime:       req.CTime,
			ExamineName: req.ExamineName,
			ExamineNum:  req.ExamineNum,
			ExamineType: req.ExamineType,
			Cover:       req.Cover,
			QrCode:      req.QrCode,
			Skey:        req.Skey,
			Describe:    req.Describe,
			LookRange:   req.LookRange,
			EnableType:  req.EnableType,
		}
		if !dao.Update(&video) {
			c.JSON(http.StatusOK, gin.H{
				"code": 1000,
				"msg":  "视频修改失败",
				"data": nil,
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "视频修改成功",
			"data": video,
		})
	} else {
		video = model.Video{
			Id:         req.Id,
			VName:      req.VName,
			Author:     c.GetUint("userId"),
			AuthorNum:  req.AuthorNum,
			MajorOne:   req.MajorOne,
			MajorTwo:   req.MajorTwo,
			CName:      req.CName,
			CNum:       req.CNum,
			CTime:      req.CTime,
			Cover:      req.Cover,
			QrCode:     req.QrCode,
			Skey:       req.Skey,
			Describe:   req.Describe,
			LookRange:  req.LookRange,
			EnableType: req.EnableType,
		}
		if !dao.Update(&video) {
			c.JSON(http.StatusOK, gin.H{
				"code": 1000,
				"msg":  "管理员视频修改失败",
				"data": nil,
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "管理员视频修改成功",
			"data": video,
		})
	}

}
func ListVideo(c *gin.Context) {
	var req request.ListVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	//req.Describe
	var video []model.Video
	get := dao.GetListVideo()
	for _, i := range get {
		video = append(video, model.Video{
			Id:          i.Id,
			VName:       i.VName,
			Author:      i.Author,
			AuthorNum:   i.CNum,
			MajorOne:    i.MajorOne,
			MajorTwo:    i.MajorTwo,
			CName:       i.CName,
			CNum:        i.CNum,
			CTime:       i.CTime,
			ExamineName: i.ExamineName,
			ExamineNum:  i.ExamineNum,
			ExamineType: i.ExamineType,
			Cover:       i.Cover,
			QrCode:      i.QrCode,
			Skey:        i.Skey,
			Describe:    i.Describe,
			LookRange:   i.LookRange,
			EnableType:  i.EnableType,
			CreatedAt:   i.CreatedAt,
			UpdateAt:    i.UpdateAt,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频列表展示成功",
		"data": get,
	})

}
func DelVideo(c *gin.Context) {
	var req request.DelVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var video model.Video
	if !dao.GetOneById(req.Id, &video) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "视频详情展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频审核详情展示成功",
		"data": map[string]interface{}{
			"ExamineName": video.ExamineName,
			"ExamineNum":  video.ExamineNum,
			"ExamineType": video.ExamineType,
		},
	})

	video = model.Video{
		Id: req.Id,
	}
	if !dao.Delete(&video) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "视频删除失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频删除成功",
		"data": true,
	})

}

func Examine(c *gin.Context) {
	var req request.ExamineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	var video []model.Video
	get := dao.GetListAuthor()
	for _, i := range get {
		video = append(video, model.Video{

			ExamineName: i.ExamineName,
			ExamineNum:  i.ExamineNum,
			ExamineType: i.ExamineType,
		})
	}
	//dao.GetListByFields(&model.Video{}, &video)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "审核人员展示成功",
		"data": video,
	})

}
